<!--
 * @Author: wangze@linkstec.com
 * @Date: 2022-03-03 14:21:09
 * @Function: 添加自选或者添加购物车调用 this.FundAction_addAction
-->
<script>

import ConfirmBeforeBuy from '@/components/businessComponents/ConfirmBeforeBuy';

export default {
  name: 'FundActionMixin',
  components: {
    ConfirmBeforeBuy
  },
  data () {
    return {
      FundAction_message: null
    };
  },
  methods: {
    /**
     * 检查是否已经自选 || 是否已经购物车
     * @param {*} type 自选池:s，购物车:p
     * @param {*} prodCode 产品代码
     * @param {*} prodName 产品名称
     * @param {*} typeCode 产品一级分类 1 股票型基金 2 混合型基金 3 债券型基金 4 货币基金 5 QDII基金 6 其他基金
     */
    FundAction_addAction ({ type, prodCode, prodName, typeCode }) {
      // 根据pro_code判断是否在购物车和自选  自选池:s，购物车:p
      this.$api.cart.getIsInCartOrSelfSelected({
        TYPE: type,
        PRO_LIST: [{
          PRO_CODE: prodCode
        }]
      }).then(res => {
        const data = res.data || [];
        const flag = data.length > 0;
        if (type === 's') { // 自选
          if (flag) { // 已添加自选
            this.FundAction_isSelected();
          } else {
            this.FundAction_addSelect(prodCode, prodName, typeCode);
          }
        } else if (type === 'p') { // 购物车
          if (flag) { // 已添加购物车
            this.FundAction_isPreordered();
          } else {
            this.FundAction_addPreorder(prodCode, prodName, typeCode);
          }
        }
      });
    },

    // —————————————————————购物车相关 start—————————————————————————————————————————————————————————————————
    // 已经添加过购物车
    FundAction_isPreordered () {
      const h = this.$createElement;
      this.FundAction_message = this.$message({
        message: h('div', { style: 'color: #13ce66;display: flex;align-items: center;' }, [
          `当前产品已经在购物车，`,
          h('el-link', { props: { underline: false, type: 'primary' }, on: { click: this.FundAction_toPreorder } }, '点击查看')
        ]),
        type: 'success'
      });
    },

    // 添加购物车调用
    FundAction_addPreorder (prodCode, prodName, typeCode) {
      this.$refs.confirmBeforeBuy.show({ prodCode, prodName, typeCode });
    },
    // 签署同意书之后
    FundAction_afterConfirm ({ prodCode, prodName, typeCode, serialId }, callback) {
      this.$api.cart.operate({
        RECORD_STS: 'a',
        PRO_CODE: prodCode,
        PRO_NAME: prodName,
        TYPE_CODE: typeCode, // typeCode,
        SERIAL_ID_LIST: [serialId]
      }).then(res => {
        this.FundAction_addPreorderSuccess(typeCode, callback);
      });
    },
    // 添加自选成功
    FundAction_addPreorderSuccess (typeCode, callback) {
      const h = this.$createElement;
      this.FundAction_message = this.$message({
        message: h('div', { style: 'color: #13ce66;display: flex;align-items: center;' }, [
          `成功加入基金购物车，`,
          h('el-link', { props: { underline: false, type: 'primary' }, on: { click: this.FundAction_toPreorder.bind(this, typeCode) } }, '点击查看')
        ]),
        type: 'success'
      });
      callback && callback();
    },
    // 到购物车页面
    FundAction_toPreorder (type) {
      this.FundAction_message && this.FundAction_message.close();
      this.FundAction_message = null;
      this.$router.push({
        path: '/account/cart',
        query: { type }
      });
    },
    // —————————————————————购物车相关 end—————————————————————————————————————————————————————————————————

    // —————————————————————自选相关 start—————————————————————————————————————————————————————————————————

    // 已经添加过自选
    FundAction_isSelected () {
      const h = this.$createElement;
      this.FundAction_message = this.$message({
        message: h('div', { style: 'color: #13ce66;display: flex;align-items: center;' }, [
          `当前产品已经在自选池，`,
          h('el-link', { props: { underline: false, type: 'primary' }, on: { click: this.FundAction_toSelect } }, '点击查看')
        ]),
        type: 'success'
      });
    },

    // 添加自选调用
    FundAction_addSelect (prodCode, prodName, typeCode, callback) {
      this.$api.selfSelected.operate({
        TYPE: 'add',
        PRO_LIST: [{
          PRO_CODE: prodCode,
          PRO_NAME: prodName,
          TYPE_CODE: typeCode // typeCode,
        }]
      }).then(res => {
        this.FundAction_addSelectSuccess(typeCode, callback);
      });
    },
    // 添加自选成功
    FundAction_addSelectSuccess (typeCode, callback) {
      const h = this.$createElement;
      this.FundAction_message = this.$message({
        message: h('div', { style: 'color: #13ce66;display: flex;align-items: center;' }, [
          `成功加入自选，`,
          h('el-link', { props: { underline: false, type: 'primary' }, on: { click: this.FundAction_toSelect.bind(this, typeCode) } }, '点击查看')
        ]),
        type: 'success'
      });
      callback && callback();
    },
    // 到自选页面
    FundAction_toSelect (type) {
      this.FundAction_message && this.FundAction_message.close();
      this.FundAction_message = null;
      this.$router.push({
        path: '/account/selfSelected',
        query: { type }
      });
    }

    // —————————————————————自选相关 end—————————————————————————————————————————————————————————————————

  }

};
</script>
